﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RentalCalcV6
{
    /// <summary>
    /// The Customer class, 
    /// one customer can hold several Rental instances, one Rental hold 1 Movie instance,which represent one customer rents serveral movies
    /// </summary>
    public class Customer
    {
        private string _name;

        /// <summary>
        /// A list of Rental instances, which the customer hold
        /// </summary>
        private List<Rental> _rentals = new List<Rental>();

        /// <summary>
        /// Create a new instance of the Customer class
        /// </summary>
        /// <param name="name"></param>
        public Customer(string name)
        {
            _name = name;
        }

        /// <summary>
        /// Gets or sets the memeberPoints
        /// </summary>
        public double MemberPoints
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the customer's name
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }
        }

        /// <summary>
        /// Add a Rental instance into list
        /// </summary>
        /// <param name="arg">a Rental instance</param>
        public void AddRental(Rental arg)
        {
            _rentals.Add(arg);
        }

        /// <summary>
        /// prepare the output statement
        /// </summary>
        /// <returns>the output statement</returns>
        public string Statement()
        {  
            int SerialNum = 0;

            string result = "Rental Record for " + Name + ":" + "\n";

            foreach (Rental item in _rentals)
            {
                SerialNum++;
                result += SerialNum.ToString() + "---" + item.RentedMovie.Title + "\t Amount: " + item.GetCharge() + "\n";                
            }

            result += "Total Amount owned is " + GetTotalCharge().ToString() + "\n";
            result += "You earned " + GetTotalMemeberPoints().ToString() + " member points" + "\n";

            return result;
        }

        /// <summary>
        /// prepare the html output statement
        /// </summary>
        /// <returns>the html output statement</returns>
        public string HtmlStatement()
        {
            string result = "<H1>Rentals for <EM>" + Name + "</EM></H1><P>\n";

            foreach (Rental item in _rentals)
            {
                result += item.RentedMovie.Title + ": " + item.GetCharge().ToString() + "<BR>\n";             
            }

            // add footer line
            result += "<P>You owe <EM>" + GetTotalCharge().ToString() + "</EM><P>\n";

            result += "You earned <EM>" + GetTotalMemeberPoints().ToString() + "</EM> renter points<P>";

            return result;
        }

        /// <summary>
        /// Calculate the price of one rental
        /// </summary>
        /// <param name="item">the rental to be calculated</param>
        /// <returns>the price</returns>
        private static double AmountFor(Rental item)
        {
            return item.GetCharge();
        }

        /// <summary>
        /// Calculate the total charge
        /// </summary>
        /// <returns>total charge</returns>
        private double GetTotalCharge()
        {
            double result = 0;

            foreach (Rental item in _rentals)
            {
                result += item.GetCharge();                
            }

            return result;
        }

        /// <summary>
        /// Calculate the total memeber points
        /// </summary>
        /// <returns>total member points</returns>
        private int GetTotalMemeberPoints()
        {
            int result = 0;

            foreach (Rental item in _rentals)
            {
                result += item.GetPoints();
            }

            return result;
        }
    }
}
